package com.wchen.gdoc.generate.mapping.resolver;

import com.wchen.gdoc.generate.mapping.data.ParamData;
import com.wchen.gdoc.generate.resource.structure.FieldStructure;
import com.wchen.gdoc.generate.resource.structure.ObjectStructure;
import com.wchen.gdoc.generate.util.StructureUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 对象结构解析
 *
 * @author  wchen
 * @date    2019/11/7
 */
public class ObjectStructureResolver extends AbsObjectStructureResolver implements TypeStructureResolver {
    public ObjectStructureResolver() {
        super();
    }

    /**
     * 判断是否支持解析器解析
     *
     * @param classType
     * @return
     */
    @Override
    public Boolean isSupport(String classType) {
        return Boolean.TRUE;
    }

    /**
     * 参数解析
     * @param objectStructure
     * @return
     */
    @Override
    public ParamData doResolve(ObjectStructure objectStructure) {
        String paramName = StructureUtil.getTypeAlias(objectStructure);
        String commentText = StructureUtil.getCommentText(objectStructure);
        if(ENABLE_DEBUG) {
            LOGGER.info(String.format("ObjectStructureResolver begin.  type:%s, name:%s", objectStructure.getType(), objectStructure.getSimpleName()));
        }

        List<ParamData> paramDataChildren = new ArrayList<>();
        if("T".equals(objectStructure.getType())){
            return super.createParamData(paramName, objectStructure.getType(), null, "{}", commentText, Boolean.FALSE);
        }

        for (FieldStructure fieldStructure : objectStructure.getFieldList()) {
            ParamData fieldParamData = super.doResolverChain(fieldStructure);
            paramDataChildren.add(fieldParamData);
        }

        return super.createParamData(paramName, objectStructure.getType(), null, paramDataChildren, commentText, Boolean.TRUE);
    }

}
